//
//  LaunchViewController.m
//  CangLongWnnianli
//
//  Created by 暗流澎涌 on 2025/7/2.
//

#import "LaunchViewController.h"
#import "WelcomeViewController.h"
#import <Masonry/Masonry.h>
#import "MainTabBarController.h"
@interface LaunchViewController ()

@property (nonatomic, strong) UIView *logoContainer;
@property (nonatomic, strong) UIImageView *logoImageView;
@property (nonatomic, strong) UILabel *appNameLabel;
@property (nonatomic, strong) UILabel *sloganLabel;

@end

@implementation LaunchViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupUI];
    [self startAnimation];
}

- (void)setupUI {
    // 设置渐变背景
    CAGradientLayer *gradientLayer = [CAGradientLayer layer];
    gradientLayer.frame = self.view.bounds;
    gradientLayer.colors = @[
        (id)[UIColor colorWithRed:0.773 green:0.188 blue:0.188 alpha:1.0].CGColor,
        (id)[UIColor colorWithRed:0.839 green:0.620 blue:0.180 alpha:1.0].CGColor
    ];
    gradientLayer.startPoint = CGPointMake(0, 0);
    gradientLayer.endPoint = CGPointMake(1, 1);
    [self.view.layer insertSublayer:gradientLayer atIndex:0];
    
    // Logo容器
    self.logoContainer = [[UIView alloc] init];
    [self.view addSubview:self.logoContainer];
    
    // Logo图片视图
    self.logoImageView = [[UIImageView alloc] init];
    self.logoImageView.contentMode = UIViewContentModeScaleAspectFit;
    self.logoImageView.image = [self createLogoImage];
    // 设置圆形效果
    self.logoImageView.layer.cornerRadius = 80; // 设置为宽度的一半
    self.logoImageView.layer.masksToBounds = YES;
    [self.logoContainer addSubview:self.logoImageView];
    
    // App名称
    self.appNameLabel = [[UILabel alloc] init];
    self.appNameLabel.text = @"农历生日记录";
    self.appNameLabel.font = [UIFont boldSystemFontOfSize:32];
    self.appNameLabel.textColor = [UIColor whiteColor];
    self.appNameLabel.textAlignment = NSTextAlignmentCenter;
    [self.logoContainer addSubview:self.appNameLabel];
    
    // 标语
    self.sloganLabel = [[UILabel alloc] init];
//    self.sloganLabel.text = @"传承千年智慧 · 择吉开启未来";
    self.sloganLabel.text = @"农历· 生日记录";
    self.sloganLabel.font = [UIFont systemFontOfSize:16];
    self.sloganLabel.textColor = [UIColor colorWithRed:0.996 green:0.988 blue:0.749 alpha:1.0];
    self.sloganLabel.textAlignment = NSTextAlignmentCenter;
    [self.logoContainer addSubview:self.sloganLabel];
    
    [self setupConstraints];
}

- (void)setupConstraints {
    [self.logoContainer mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(self.view);
        make.width.mas_equalTo(200);
        make.height.mas_equalTo(300);
    }];
    
    [self.logoImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.logoContainer);
        make.centerX.equalTo(self.logoContainer);
        make.width.height.mas_equalTo(160);
    }];
    
    [self.appNameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.logoImageView.mas_bottom).offset(24);
        make.left.right.equalTo(self.logoContainer);
        make.height.mas_equalTo(40);
    }];
    
    [self.sloganLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.appNameLabel.mas_bottom).offset(8);
        make.left.right.equalTo(self.logoContainer);
        make.height.mas_equalTo(20);
    }];
}

- (UIImage *)createLogoImage {
    // 使用实际的logo图片
    UIImage *logoImage = [UIImage imageNamed:@"spaiamge"]; // 请将你的logo图片命名为"AppLogo"并添加到Assets.xcassets
    
    // 如果没有找到图片，则使用程序生成的默认logo
    if (!logoImage) {
        // 创建一个简单的Logo图片作为备用
        CGSize size = CGSizeMake(160, 160);
        UIGraphicsBeginImageContextWithOptions(size, NO, 0);
        CGContextRef context = UIGraphicsGetCurrentContext();
        
        // 绘制圆形背景
        CGContextSetFillColorWithColor(context, [UIColor whiteColor].CGColor);
        CGContextFillEllipseInRect(context, CGRectMake(4, 4, 152, 152));
        
        // 绘制渐变边框
        CGContextSetStrokeColorWithColor(context, [UIColor colorWithRed:0.839 green:0.620 blue:0.180 alpha:1.0].CGColor);
        CGContextSetLineWidth(context, 4);
        CGContextStrokeEllipseInRect(context, CGRectMake(4, 4, 152, 152));
        
        logoImage = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
    }
    
    return logoImage;
}

- (void)startAnimation {
    // Logo缩放动画
    self.logoImageView.transform = CGAffineTransformMakeScale(0.5, 0.5);
    self.appNameLabel.alpha = 0;
    self.sloganLabel.alpha = 0;
    
    [UIView animateWithDuration:1.0 delay:0.5 options:UIViewAnimationOptionCurveEaseOut animations:^{
        self.logoImageView.transform = CGAffineTransformIdentity;
        self.appNameLabel.alpha = 1;
        self.sloganLabel.alpha = 1;
    } completion:^(BOOL finished) {
        // 2秒后跳转到欢迎页
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [self navigateToWelcome];
        });
    }];
}

- (void)navigateToWelcome {   // 检查是否是首次启动
    BOOL isFirstLaunch = ![[NSUserDefaults standardUserDefaults] boolForKey:@"hasLaunchedBefore"];
    
    if (isFirstLaunch) {
        // 首次启动，显示欢迎页面
        WelcomeViewController *welcomeVC = [[WelcomeViewController alloc] init];
        welcomeVC.modalPresentationStyle = UIModalPresentationFullScreen;
        welcomeVC.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
        [self presentViewController:welcomeVC animated:YES completion:nil];
    } else {
        // 非首次启动，直接进入主界面
        MainTabBarController *mainTabBar = [[MainTabBarController alloc] init];
        mainTabBar.modalPresentationStyle = UIModalPresentationFullScreen;
        mainTabBar.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
        [self presentViewController:mainTabBar animated:YES completion:nil];
    }}

@end 
